package model

import (
	"github.com/gomodule/redigo/redis"
	"strconv"
	"tizhi_front/nspel"
)

type CourseCollect struct {
	Id        int64 `gorm:"AUTO_INCREMENT;primary_key;column:id"`
	StudentId int64 `gorm:"column:student_id"`
	CourseId  int64 `gorm:"column:course_id"`
}

func (*CourseCollect) TableName() string {
	return "course_collect"
}

type CourseCollectE struct{}

func (*CourseCollectE) TableName() string {
	return "course_collect"
}

func (c *CourseCollectE) SelectAllByStudentId(studentId int64) (courseCollects []CourseCollect2, err error) {
	key := "course_collect2.student_id:" + strconv.FormatInt(studentId, 10) + ".all"
	err = nspel.RedisGetGob(key, &courseCollects)
	if err == nil {
		return
	}
	if err != redis.ErrNil {
		nspel.Error(err)
	} else {
		err = nil
	}
	if err = nspel.GetOrm().Select("course_id").Where("student_id = ?", studentId).Find(&courseCollects).Error; err != nil {
		return
	}
	nspel.RedisSetGob(key, courseCollects)
	return
}

func (c *CourseCollectE) SelectByStudentIdCourseId(studentId int64, courseId int64) (err error) {
	var courseCollect CourseCollect0
	key := "course_collect0.student_id:" + strconv.FormatInt(studentId, 10) + ".course_id:" + strconv.FormatInt(courseId, 10)
	err = nspel.RedisGetGob(key, &courseCollect)
	if err == nil {
		return
	}
	if err != redis.ErrNil {
		nspel.Error(err)
	} else {
		err = nil
	}
	if err = nspel.GetOrm().Select("id").Where("student_id = ?", studentId).Where("course_id = ?", courseId).First(&courseCollect).Error; err != nil {
		return
	}
	nspel.RedisSetGob(key, courseCollect)
	return
}

func (c *CourseCollectE) SelectCountByCourseId(courseId int64) (count int64, err error) {
	key := "course_collect.course_id:" + strconv.FormatInt(courseId, 10) + ".count"
	err = nspel.RedisGetInt64(key, &count)
	if err == nil {
		return
	}
	if err != redis.ErrNil {
		nspel.Error(err)
	} else {
		err = nil
	}
	if err = nspel.GetOrm().Table(c.TableName()).Where("course_id = ?", courseId).Count(&count).Error; err != nil {
		return
	}
	nspel.RedisSetInt64(key, count)
	return
}

type CourseCollect0 struct {
	Id int64 `gorm:"primary_key;column:id"`
}

func (*CourseCollect0) TableName() string {
	return "course_collect"
}

func (c *CourseCollect0) SelectByStudentIdCourseId(studentId int64, courseId int64) (err error) {
	key := "course_collect0.student_id:" + strconv.FormatInt(studentId, 10) + ".course_id:" + strconv.FormatInt(courseId, 10)
	err = nspel.RedisGetGob(key, c)
	if err == nil {
		return
	}
	if err != redis.ErrNil {
		nspel.Error(err)
	} else {
		err = nil
	}
	if err = nspel.GetOrm().Select("id").Where("student_id = ?", studentId).Where("course_id = ?", courseId).First(c).Error; err != nil {
		return
	}
	nspel.RedisSetGob(key, c)
	return
}

func (c *CourseCollect0) Delete(studentId int64, courseId int64) (err error) {
	err = nspel.GetOrm().Delete(c).Error
	if err != nil {
		return
	}
	_ = nspel.RedisKeysDelete("course_collect0.student_id:"+strconv.FormatInt(studentId, 10)+".course_id:"+strconv.FormatInt(courseId, 10), "course_collect.course_id:"+strconv.FormatInt(courseId, 10)+".count")
	return
}

type CourseCollect1 struct {
	StudentId int64 `gorm:"column:student_id"`
	CourseId  int64 `gorm:"column:course_id"`
}

func (*CourseCollect1) TableName() string {
	return "course_collect"
}

func (c *CourseCollect1) Insert() (err error) {
	err = nspel.GetOrm().Create(c).Error
	if err != nil {
		return
	}
	_ = nspel.RedisKeysDelete("course_collect.course_id:" + strconv.FormatInt(c.CourseId, 10) + ".count")
	return
}

type CourseCollect2 struct {
	CourseId int64 `gorm:"column:course_id"`
}

func (*CourseCollect2) TableName() string {
	return "course_collect"
}
